package com.firefighting.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.firefighting.dao.MaintenancePlanMapper;
import com.firefighting.dao.ReportMapper;
import com.firefighting.entity.MaintenancePlan;
import com.firefighting.entity.Report;

@Service
public class ReportService {
	@Autowired
	private ReportMapper reportMapper;
	@Autowired
	private MaintenancePlanMapper maintenancePlanMapper;
	@Transactional(rollbackFor = Exception.class)
	public void insertReport(String contractId, int type, String year, String month, int season , String root, String pdfname){
		String planExcuteTime = null;
		if(type == 1){//月
			
				planExcuteTime = year+month;
				
			
		}else if(type == 2){//季度
			if(season < 4){
				planExcuteTime = year+"0"+season*3;
			}else if(season == 4){
				planExcuteTime = year+"12";
			}
			
		}else if(type == 3){//年
			planExcuteTime = year+"12";
		}
		Date createtime = new Date(); 
		int planId = maintenancePlanMapper.selectIdByTypeAndExcuteTime(type,planExcuteTime,Integer.parseInt(contractId));
		Report report = new Report();
		report.setPlanId(planId);
		report.setName(pdfname);
		report.setMaintableUrl(root);
		report.setCreatetime(createtime);
		reportMapper.insertSelective(report);
		MaintenancePlan maintenancePlan = new MaintenancePlan();
		maintenancePlan.setId(Integer.parseInt(contractId));
		maintenancePlan.setIsReport(true);
		maintenancePlanMapper.updateByPrimaryKeySelective(maintenancePlan);
	}
    /**
     * @author Liuxc
     * 根据合同id 查询报告
     * */
	public List<HashMap<String, Object>> getReportByContractId(int contractId) {
		// TODO Auto-generated method stub
		
		return reportMapper.getReportByContractId(contractId);
	}
}
